package cn.zyq.ykb.service.tk.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.zyq.ykb.beans.tk.Menu;
import cn.zyq.ykb.beans.tk.MenuFood;
import cn.zyq.ykb.beans.user.User;
import cn.zyq.ykb.mapper.tk.MenuFoodMapper;
import cn.zyq.ykb.mapper.tk.MenuMapper;
import cn.zyq.ykb.system.common.BaseService;
import com.github.pagehelper.PageHelper;
import cn.zyq.ykb.system.ry.util.SecurityUtils;
import cn.zyq.ykb.mapper.tk.MenuCollectMapper;
import cn.zyq.ykb.beans.tk.MenuCollect;
import cn.zyq.ykb.service.tk.MenuCollectService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.zyq.ykb.system.common.PageParam;
import com.github.pagehelper.PageInfo;


import java.time.LocalDateTime;
import java.util.UUID;
import java.util.List;

/**
 * 菜单收藏Service业务层处理
 *
 * @author SystemAuth
 * @date 2023-10-20
 */
@Service
public class MenuCollectServiceImpl  extends BaseService implements MenuCollectService {

    @Autowired
    private MenuCollectMapper menuCollectMapper;
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private MenuFoodMapper menuFoodMapper;

    /**
     * 查询菜单收藏
     *
     * @param menuCollectID 菜单收藏主键
     * @return 菜单收藏
     */
    @Override
    public MenuCollect selectMenuCollectByMenuCollectID(Integer menuCollectID) {
        MenuCollect menuCollect = menuCollectMapper.selectByPrimaryKey(menuCollectID);
        if (menuCollect.getDeleted() == 1) {
            return null;
        }
        return menuCollect;
    }

    /**
     * 查询菜单收藏
     *
     * @param serviceID 业务ID
     * @return 菜单收藏
     */
    @Override
    public MenuCollect selectMenuCollectByServiceID(String serviceID) {
        MenuCollect param = new MenuCollect();
        param.setDeleted(0);
        param.setServiceID(serviceID);
        return this.menuCollectMapper.selectOne(param);
    }

    /**
     * 查询菜单收藏列表
     *
     * @param pageParam
     * @return 菜单收藏
     */
    @Override
    public PageInfo<MenuCollect> selectMenuCollectList(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        List<MenuCollect> list = this.menuCollectMapper.list(pageParam.getKv());
        for (MenuCollect menuCollect : list) {
            Menu menu = this.menuMapper.selectByPrimaryKey(menuCollect.getMenuID());
            MenuFood pa = new MenuFood();
            pa.setMenuID(menuCollect.getMenuID());
            pa.setDeleted(0);
            List<MenuFood> select = this.menuFoodMapper.select(pa);
            menu.setMenuFood(select);
            menuCollect.setMenu(menu);


        }
        return new PageInfo<>(list);
    }

    /**
     * 查询菜单收藏列表
     *
     * @param pageParam
     * @return 菜单收藏
     */
    @Override
    public List<MenuCollect> selectMenuCollectAllList(PageParam pageParam) {
        pageParam.getKv().put("deleted", 0);
        return this.menuCollectMapper.list(pageParam.getKv());
    }
    /**
     * 新增菜单收藏
     *
     * @param param 菜单收藏
     * @return 结果
     */
    @Override
    public int insertMenuCollect(MenuCollect param) {
        MenuCollect pa = new MenuCollect();
        pa.setTUserID(param.getTUserID());
        pa.setMenuID(param.getMenuID());
        MenuCollect menuCollect = this.menuCollectMapper.selectOne(pa);
        if (BeanUtil.isEmpty(menuCollect)){
            param.setState(1);
            this.setCreateBaseData(param, new User());
            return this.menuCollectMapper.insertSelective(param);
        }else {
            MenuCollect updateInstance = this.getUpdateInstance(MenuCollect.class);
            updateInstance.setMenuCollectID(menuCollect.getMenuCollectID());
            if (menuCollect.getState().equals(1)){
                updateInstance.setState(2);
                this.setUpdateBaseData(updateInstance,new User());
                this.menuCollectMapper.updateByPrimaryKeySelective(updateInstance);
                return 2;
            }else {
                updateInstance.setState(1);
                this.setUpdateBaseData(updateInstance,new User());
                this.menuCollectMapper.updateByPrimaryKeySelective(updateInstance);
                return 1;
            }
        }

    }

    /**
     * 修改菜单收藏
     *
     * @param param 菜单收藏
     * @return 结果
     */
    @Override
    public int updateMenuCollect(MenuCollect param) {
        this.setUpdateBaseData(param, new User());
        return this.menuCollectMapper.updateByPrimaryKeySelective(param);
    }

    /**
     * 批量删除菜单收藏
     *
     * @param menuCollectIDs 需要删除的菜单收藏主键
     * @return 结果
     */
    @Override
    public int deleteMenuCollectByMenuCollectIDs(Integer[] menuCollectIDs) {
        int i = 0;
        for (Integer menuCollectID : menuCollectIDs) {
            i += this.deleteMenuCollectByMenuCollectID(menuCollectID);
        }
        return i;
    }

    /**
     * 删除菜单收藏信息
     *
     * @param menuCollectID 菜单收藏主键
     * @return 结果
     */
    @Override
    public int deleteMenuCollectByMenuCollectID(Integer menuCollectID) {
        MenuCollect menuCollect = this.selectMenuCollectByMenuCollectID(menuCollectID);
        if (menuCollect == null) {
            return 0;
        } else {
            MenuCollect storeInfo = new MenuCollect();
            storeInfo.setMenuCollectID(menuCollectID);
            storeInfo.setDeleted(1);
            storeInfo.setUpdateTime(LocalDateTime.now());
            this.menuCollectMapper.updateByPrimaryKeySelective(storeInfo);
            return 1;
        }
    }
}
